package com.mjk.manager.magicapi.plugin.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.mjk.manager.magicapi.core.core.config.MagicPluginConfiguration;
import com.mjk.manager.magicapi.core.core.model.Plugin;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MagicElasticSearchConfiguration implements MagicPluginConfiguration {

    @Override
    public Plugin plugin() {
        return new Plugin("ElasticSearch");
    }

    @Bean
    @ConditionalOnProperty(name = "bee.es", havingValue = "true")
    public RestClient elasticsearchClient() {
        // 创建 RestClientBuilder
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("localhost", 9200, "http") // 替换为您的 Elasticsearch 集群地址
        );
        //可以根据需要添加更多配置
        builder.setRequestConfigCallback(requestConfigBuilder ->
                requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000)
        );
        return builder.build(); // 创建 RestClientBuilder
    }

    @Bean
    public ElasticSearchModule elasticSearchModule(RestClient restHighLevelClient) {
        return new ElasticSearchModule(restHighLevelClient);
    }
}
